package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"ruku/models"
)

type MainController struct {
	beego.Controller
}

type AllRes struct {
	Cre   int
	Msg   string
	Data  interface{}
	Count int64
}

type Res struct {
	Cre  int
	Msg  string
	Data interface{}
}

func (c *MainController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	CangKu := c.GetString("CangKu")
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	off := (page - 1) * size

	var data []models.RuKu

	var count int64

	models.DB.Where("cang_ku like ?", "%"+CangKu+"%").Offset(off).Limit(size).Order(
		"cang_ku desc").Find(&data)

	marshal, _ := json.Marshal(&data)

	models.RedisDB.Set("list", marshal, 0)

	models.DB.Model(&data).Where("cang_ku like ?", "%"+CangKu+"%").Count(&count)
	c.Data["json"] = AllRes{200, "列表查询成功", data, count}
	c.ServeJSON(true)

}
